package com.fiap.voo;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import com.fiap.assento.AssentoBean;
import com.fiap.assento.AssentoDao;
import com.fiap.trecho.TrechoBean;
import com.fiap.trecho.TrechoDao;
import com.fiap.trecho.TrechoService;

public class VooService implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public VooBean save(VooBean voo) throws SQLException {
		VooDao vooDao = new VooDao();
		TrechoService trechoService = new TrechoService();
		
			if (voo.getId() > 0) {
				voo = vooDao.update(voo);
			} else {
				voo = vooDao.create(voo);
			}
			
			trechoService.updateTrecho(voo);
			return voo;
	}

	public List<VooBean> list(VooBean vooBeanFiltros) throws SQLException{
		VooDao dao = new VooDao();
		return dao.list(vooBeanFiltros);
	}

	public void delete(VooBean voo) throws SQLException{
		VooDao dao = new VooDao();
		dao.delete(voo);
	}
	
	public VooBean getVoo(VooBean voo) throws SQLException{
		VooDao dao = new VooDao();
		TrechoDao tDao = new TrechoDao();
		AssentoDao aDao = new AssentoDao();
		AssentoBean assentoBeanFiltros;
		
		voo = dao.list(voo).get(0);
		voo.setListaTrechos(tDao.list(voo));
		for(TrechoBean t: voo.getListaTrechos()){
			assentoBeanFiltros = new AssentoBean();
			assentoBeanFiltros.setIdVoo(t.getIdVoo());
			assentoBeanFiltros.setIdTrecho(t.getId());
			t.setListaAssentos(aDao.list(assentoBeanFiltros));
		}
		
		return voo;
	}
}
